/**
 * Created by meiguizhinian on 2018/7/4
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//cocos2dx class
ivy.UIFlowLayoutPanel = ivy.UIBase.extend({
	_className:"ivy.UIFlowLayoutPanel",
	ctor: function (formName, blockName, sourceControlData) {
		this._super();
		//测试层
		// this._layer = new cc.LayerColor(cc.color(255,255,255,125));
		// this._layer.setAnchorPoint(cc.p(0.5,0.5));
		// this._layer.ignoreAnchorPointForPosition(false);
		// this.addChild(this._layer, -100);

		this._childVec = [];
		if (formName && blockName && sourceControlData) {
			this.initWith(formName, blockName, sourceControlData);
		}
	},

	// UIFlowLayoutPanel(const std::string& formName, const std::string& blockName, const cc::UISourceControlData& sourceControlData);
	initWith : function(formName, blockName, sourceControlData) {
		this.setUIElementName(formName, blockName);
		let uim = ivy.UIManager.getInstance();
		let x = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosX);
		let y = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosY);
		let w = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eWidth);
		let h = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eHeight);
		let layerID = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eLayerID);
		this._layerIDInEditor = layerID;
		this.setLocalZOrder(layerID);

		this.setSrcRect(cc.rect(x, y, w, h));
		this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(x + w / 2.0, y + h / 2.0)));
		this.setSrcPosition(this.getPosition());
		this.setContentSize(cc.size(w, h));
		this.setAnchorType(ivy.UIBase.UIAnchorType.eAnchorHV);
		this._enterActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_FlowLayoutPanel_EnterAction_action]));
		this._outActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_FlowLayoutPanel_OutAction_action]));

		this._paddingX = uim.getControlFloatDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_FlowLayoutPanel_XPadding_float]);
		this._paddingY = uim.getControlFloatDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_FlowLayoutPanel_YPadding_float]);
		this._horizontalLayoutType = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_FlowLayoutPanel_LayoutType_list]);
		this._vertialLayoutType = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_FlowLayoutPanel_VerticalLayoutType_list]);

		if (sourceControlData.propertyDataFindIndex.length > ivy.DefaultSourceControlTypes.SCT_ELEMENT_FlowLayoutPanel_AllChildVerticalLayoutType_list) {
			this._childrenVerticleLayoutTyepe = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_FlowLayoutPanel_AllChildVerticalLayoutType_list]);
		}
		//////////////////////////////////////////////////////////////////////////
		//设置初始可视性 wsq-2017-3-7
		let isNotVisble = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eNotVisble);
		if (isNotVisble === 1) {
			this.setVisible(false);
		}
	},

	pauseAutoLayout : function() {
		this._autoLayout = false;
	},
	resumeAutoLayout : function() {
		this._autoLayout = true;
		this.doLayout();
	},
	count : function() {
		return this._childVec.length;
	},

	pushBack : function(control) {
		this._childVec.push(control);
		this.addElement(control);
		//处理child 锚点方式必须为居中
		control.setAnchorType(ivy.UIBase.UIAnchorType.eAnchorHV);
		if (this._autoLayout)
			this.doLayout();
	},
	remove : function(indexOrObject) {
		if (cc.isNumber(indexOrObject)) {
			let index = indexOrObject;
			if(index>=0 && index<this._childVec.length) {
				let control = this._childVec[index];
				this._childVec.splice(index, 1);
				ivy.Tools.pushFunctionToCocosThread(function(){
					control.removeFromParent();
				});
			}
		} else if (indexOrObject instanceof ivy.UIBase) {
			let control = indexOrObject;
			for (let i=0; i!==this._childVec.length; ++i) {
				if (this._childVec[i] === control) {
					this._childVec.splice(i, 1);
					ivy.Tools.pushFunctionToCocosThread(function () {
						control.removeFromParent();
					});
					return;
				}
			}
		}
	},
	removeAll : function() {
		let array = this._childVec;
		ivy.Tools.pushFunctionToCocosThread(function() {
			for (let i=0; i!==array.length; ++i) {
				array[i].removeFromParent();
			}
		});
		this._childVec = [];
	},
	doLayout : function() {
		let maxSize = this.getContentSize();
		let tempRowBase = {};
		let tempBase = [];
		//第一次遍历所有child，计算出每一行的maxWidth，maxHeight
		let  everyRowSizeVec = [];
		let cMaxWidth = 0.0;
		let cMaxHeight = 0.0;
		let totalHeight = 0.0;
		let index = 0;
		//在遍历中取得最后一行的高度
		let forLashRowHeight = 0.0;
		// 将每一行控件添加进tempRowBase[index]中， 每一行的实际宽高存入everyRowSizeVec
		for (let i=0; i!==this._childVec.length; ++i) {
			let control = this._childVec[i];
			let cSize = control.getContentSize();
			if (cMaxWidth + cSize.width > maxSize.width) {
				// 单个控件width > maxSize.width 时
				if (tempBase.length > 0) {
					tempRowBase[index] = tempBase;
					tempBase = [];
					tempBase.push(control);
					everyRowSizeVec.push(cc.size(cMaxWidth - this._paddingX, cMaxHeight));
					++index;
					totalHeight += cMaxHeight + this._paddingY;
					cMaxWidth = 0.0;
					cMaxHeight = 0.0;
					forLashRowHeight = 0.0;
				} else {
					tempBase.push(control);
					tempRowBase[index] = tempBase;
					tempBase = [];
					everyRowSizeVec.push(cSize);
					++index;
					totalHeight += cSize.height + this._paddingY;
					cMaxWidth = 0.0;
					cMaxHeight = 0.0;
					forLashRowHeight = 0.0;
					continue;
				}
			} else {
				tempBase.push(control);
			}
			cMaxWidth += cSize.width + this._paddingX;
			if (cSize.height > cMaxHeight) {
				cMaxHeight = cSize.height;
				forLashRowHeight = cMaxHeight ;
			}
		}
		if (cMaxWidth > 0 || cMaxHeight >0)
		{
			tempRowBase[index] = tempBase;
			tempBase = [];
			everyRowSizeVec.push(cc.size(cMaxWidth - this._paddingX, cMaxHeight));
		}
		if(forLashRowHeight>0) {
			totalHeight += forLashRowHeight - this._paddingY;
		}

		let FlowVerticalLayoutType = ivy.UIFlowLayoutPanel.FlowVerticalLayoutType;
		let FlowHorizontalLayoutType = ivy.UIFlowLayoutPanel.FlowHorizontalLayoutType;

		//第二次遍历求出各个child的位置并setPosition
		let startX;
		let maxHeightPos = this._childrenVerticleLayoutTyepe === FlowVerticalLayoutType.eGravityCenter ?
			maxSize.height / 2.0 - (maxSize.height - totalHeight) / 2.0 : this._childrenVerticleLayoutTyepe === FlowVerticalLayoutType.eGravityTop ?
				maxSize.height / 2.0 : maxSize.height / 2.0 - (maxSize.height - totalHeight);
		for (let row in tempRowBase) {
			if (!tempRowBase.hasOwnProperty(row)) {
				continue;
			}
			let curRowSize = everyRowSizeVec[row];

			startX = FlowHorizontalLayoutType.eAutoMiddle === this._horizontalLayoutType ? -curRowSize.width / 2.0 : FlowHorizontalLayoutType.eNormal === this._horizontalLayoutType ?-maxSize.width / 2.0 : (maxSize.width - curRowSize.width)/2.0 - curRowSize.width/2.0;
			for (let i=0; i!==tempRowBase[row].length; ++i) {
				let control = tempRowBase[row][i];
				let cSize = control.getContentSize();
				let posX = startX + cSize.width / 2.0;
				let posY = FlowVerticalLayoutType.eGravityTop === this._vertialLayoutType ? maxHeightPos - cSize.height / 2 : FlowVerticalLayoutType.eGravityCenter === this._vertialLayoutType ? maxHeightPos - curRowSize.height / 2.0 : maxHeightPos - curRowSize.height + cSize.height / 2.0;
				startX = startX + cSize.width + this._paddingX;
				control.setPosition(posX, posY);
				control.setSrcPosition(control.getPosition());
			}
			maxHeightPos += -curRowSize.height - this._paddingY;
		}

		if (totalHeight > maxSize.height) {
			this.setContentSize(cc.size(maxSize.width, totalHeight));
		}
		//this._layer.setContentSize(this.getContentSize());
	},
	getItems : function() {
		return this._childVec;
	},
	getItemCount : function() {
		return this.count();
	},
	getItem : function(index, T=ivy.UIBase) {
		if (index >= 0 && index < this._childVec.length) {
			let child = this._childVec[index];
			if (child instanceof T) {
				return child;
			}
		}
		return null;
	},
	onChildCreateComplete : function() {
		let children = this.getChildren();
		this._childVec = [];
		for (let i=0; i!==children.length; ++i) {
			let child = children[i];
			if (child instanceof ivy.UIBase) {
				this._childVec.push(child);
				//	//处理child 锚点方式必须为居中
				child.setAnchorType(ivy.UIBase.UIAnchorType.eAnchorHV);
			}
		}
		children.sort(function (lhs, rhs) {
			return lhs.getLocalZOrder() - rhs.getLocalZOrder();
		});
		this.doLayout();
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////protected:
	_horizontalLayoutType : 0,
	_vertialLayoutType : 1,
	_childrenVerticleLayoutTyepe : 1,
	_autoLayout : true,
	_childVec : null,//std::vector<UIBase*>
	_paddingX : 0,
	_paddingY : 0,
});

ivy.UIFlowLayoutPanel.create = function (formName, blockName, sourceControlData) {
	return new ivy.UIFlowLayoutPanel(formName, blockName, sourceControlData);
};

ivy.UIFlowLayoutPanel.FlowHorizontalLayoutType = {
	eNormal     : 0,
	eAutoMiddle : 1,
	eRight      : 2
};

ivy.UIFlowLayoutPanel.FlowVerticalLayoutType = {
	eGravityTop     : 0,
	eGravityCenter  : 1,
	eGravityBottom  : 2
};

